package com.shengqin.listeners;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.fastjson.JSON;
import com.shengqin.entity.School;
import com.shengqin.entity.SysHealthProject;
import com.shengqin.pojo.school.SchoolImportParam;
import com.shengqin.service.ISchoolService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
public class SchoolListener implements ReadListener<SchoolImportParam> {

    private final ISchoolService schoolService;
    private Set<String> errorList;
    private final List<SchoolImportParam> cachedDataList = new ArrayList<>();

    public SchoolListener(ISchoolService ss, Set<String> errors) {
        this.schoolService = ss;
        this.errorList = errors;
    }

    @Override
    public void invoke(SchoolImportParam data, AnalysisContext context) {
        ReadRowHolder readRowHolder = context.readRowHolder();
        Integer rowIndex = readRowHolder.getRowIndex();
        log.info("解析到行号为{},数据:{}",rowIndex, JSON.toJSONString(data));
        cachedDataList.add(data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        schoolService.onBatchSave(cachedDataList, errorList);
    }
}
